package com.xudow.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.cart.CartItem;
import com.activeshare.edu.ucenter.models.cart.ShoppingCartWithCourse;
import com.activeshare.edu.ucenter.models.course.CourseWithOtherInfo;
import com.makeramen.roundedimageview.RoundedImageView;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.model.CartModle;
import com.xudow.app.model.Coures;
import com.xudow.app.newui.bean.LsCatMessage;
import com.xudow.app.ui.ChoseChildActivity;
import com.xudow.app.ui.CourseDetailActivity;
import com.xudow.app.util.ImageUtils;

import java.util.List;

/**
 * 作者：zhouXJ on 2016/5/3 15:15
 */
public class CartListAdapter extends BaseExpandableListAdapter {
    private Context cnt;
    private List<LsCatMessage.CartItemsEntity> list;
    private AdapterCallBack callback;
    private boolean isEdit=false;//编辑状态标记

    public void setEdFlag(boolean b){
        isEdit=b;
        notifyDataSetChanged();
    }
    public void setCallback(AdapterCallBack call){
        this.callback=call;
    }
    public CartListAdapter(Context context,List<LsCatMessage.CartItemsEntity> list){
        this.cnt = context;
        this.list = list;
    }
    public void setData(List<LsCatMessage.CartItemsEntity> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        if (null==list){
            return 0;
        }
        return list.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return list.get(i).getList().size();
    }

    @Override
    public Object getGroup(int i) {
        return list.get(i);
    }

    @Override
    public Object getChild(int i, int j) {
        return list.get(i).getList().get(j);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int j) {
        return j;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        if (null==list){
            return null;
        }
        final LsCatMessage.CartItemsEntity cm=list.get(i);

        if ( null==cm.getList()){
            return null;
        }
        if (cm.getList().size()<1){
            return null;
        }
        GroupHolder holder=new GroupHolder();
        if (view==null){
            view=View.inflate(cnt, R.layout.cartlist_group_item,null);
            holder.name=(TextView)view.findViewById(R.id.name);
            holder.schoolImag=(RoundedImageView)view.findViewById(R.id.school_img);
            holder.schoolImag.setCornerRadius((float)5);
            holder.selectImg=(ImageView)view.findViewById(R.id.select_img);
            view.setTag(holder);
        }else
            holder=(GroupHolder)view.getTag();
        if (null!=cm.getName()&&!"null".equals(cm.getName()))
            holder.name.setText(cm.getName());


//        if (null!=cm.getGroupimgpath()){
//            //TODO 网络加载教育单位图片
//            String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, cm.getGroupimgpath());
//            ImageUtils.loadImage(cnt, holder.schoolImag, imageUrl, R.mipmap.ico_school);
//        }
        if (cm.isSelect){
            holder.selectImg.setImageResource(R.mipmap.btn_cart_checked);
        }else
            holder.selectImg.setImageResource(R.mipmap.btn_cart_unchecked);

        holder.selectImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cm.isSelect=!cm.isSelect;
                ImageView img=(ImageView)view;
                if (cm.isSelect){
                    img.setImageResource(R.mipmap.btn_cart_checked);
                    selectAll(i,true);
                }else {
                    img.setImageResource(R.mipmap.btn_cart_unchecked);
                    selectAll(i,false);
                }
                //调用activity计算价格
                callback.getCouresPrice();
                callback.isAllselect();
            }


        });
        return view;
    }

    private String getString(String obj) {
        if (null==obj||"null".equals(obj))
            return "";
        else
            return obj;
    }

    private void selectAll(int index,boolean b) {
        List<LsCatMessage.CartItemsEntity.ListEntity> clist=list.get(index).getList();
        for (LsCatMessage.CartItemsEntity.ListEntity c: clist) {
            c.isSelect=b;
        }
        notifyDataSetChanged();
    }
    class ChildHolder{
        public RoundedImageView courceImg;
        public TextView name;
        public TextView actualPrice;
        public TextView price;
        public TextView studentName;
        public TextView courceType;
        public ImageView selectImg;
        public RelativeLayout edlayout,displayout;//编辑布局
        public TextView edName;
        public ImageView edImg;
        public RelativeLayout rl;//课程布局点击进入课程详情
      LinearLayout child_select_ll;
    }

    class GroupHolder{
        public ImageView selectImg;
        public RoundedImageView schoolImag;
        public TextView name;
    }
    @Override
    public View getChildView(int i, int j, boolean b, View view, ViewGroup viewGroup) {
        if (null==list){
            return null;
        }
        ChildHolder holder=new ChildHolder();
        if (null==view){
            view=View.inflate(cnt,R.layout.cartlist_child_item,null);
            holder.courceImg=(RoundedImageView)view.findViewById(R.id.cource_img);
            holder.courceImg.setCornerRadius((float)5);
            holder.selectImg=(ImageView)view.findViewById(R.id.child_select_img);
            holder.child_select_ll=(LinearLayout)view.findViewById(R.id.child_select_ll);
            holder.name=(TextView)view.findViewById(R.id.courceName_tv);
            holder.actualPrice=(TextView)view.findViewById(R.id.actual_price_tv);
            holder.price=(TextView)view.findViewById(R.id.price_tv);
            holder.courceType=(TextView)view.findViewById(R.id.courceType_tv);
            holder.studentName=(TextView)view.findViewById(R.id.studentName_tv);
            holder.edlayout=(RelativeLayout)view.findViewById(R.id.ed_layout);
            holder.displayout=(RelativeLayout)view.findViewById(R.id.edcomplate_layout);
            holder.edName=(TextView)view.findViewById(R.id.ed_name_tv);
            holder.edImg=(ImageView)view.findViewById(R.id.ed_student_img);
            holder.rl=(RelativeLayout)view.findViewById(R.id.course_rl);
            view.setTag(holder);
        }else
            holder=(ChildHolder)view.getTag();
        final LsCatMessage.CartItemsEntity.ListEntity coures=list.get(i).getList().get(j);
        LsCatMessage.CartItemsEntity.ListEntity.CourseWithOtherInfoEntity info=coures.getCourseWithOtherInfo();
        if (null!=info.getThumbnail()){
            //TODO 异步加载课程图片
            String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, info.getThumbnail());
            ImageUtils.loadImage(cnt, holder.courceImg, imageUrl, R.mipmap.def_portrait);
        }
        holder.name.setText(info.getName());
        holder.actualPrice.setText("¥" + info.getActualPrice() + "0");
        holder.price.setText("¥" + info.getCoursePrice() + "0");
        holder.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);//中划线
        if (info.getType()==1)
            holder.courceType.setText("一对一");
        else if (info.getType()==2)
            holder.courceType.setText("辅导班");
        holder.studentName.setText(coures.getStudentName());
        holder.edName.setText("就读学生: " + coures.getStudentName());

        //编辑界面控制
        if (isEdit){
            holder.edlayout.setVisibility(View.VISIBLE);
            holder.displayout.setVisibility(View.GONE);
            holder.edlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //TODO 选学生跳转
                    Intent inten=new Intent(cnt, ChoseChildActivity.class);
                    inten.putExtra("studentId",coures.getStudentId());
                    inten.putExtra("itemId",coures.getId());
                    cnt.startActivity(inten);
                }
            });
        }else{
            holder.edlayout.setVisibility(View.GONE);
            holder.displayout.setVisibility(View.VISIBLE);
        }

        if (coures.isSelect){
            holder.selectImg.setImageResource(R.mipmap.btn_cart_checked);
        }else
            holder.selectImg.setImageResource(R.mipmap.btn_cart_unchecked);

        holder.child_select_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                coures.isSelect = !coures.isSelect;
                notifyDataSetChanged();
                callback.getCouresPrice();
                callback.isAllselect();
            }
        });
        holder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent inten=new Intent(cnt,CourseDetailActivity.class);
                inten.putExtra("courseId",coures.getCourseId());
                cnt.startActivity(inten);

            }
        });
        return view;
    }


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    /**
     * 价格计算回调
     */
    public interface AdapterCallBack{
        void getCouresPrice();
        void isAllselect();
    }
}
